const puppeteer = require('puppeteer');
const axios = require('axios');

async function main() {
  const fav_folders = {
    url: 'https://api.bilibili.com/x/v3/fav/folder/created/list-all',
    data: {
      up_mid: 44120780
    },
    method: 'GET'
  }

  const collection_folders = {
    url: 'https://api.bilibili.com/x/v3/fav/folder/collected/list',
    data: {
      pn: 1,
      ps: 20,
      up_mid: 44120780,
      platform: 'web'
    },
    method: 'GET'
  }

  const fac_folder_detail = {
    url: 'https://api.bilibili.com/x/v3/fav/resource/list',
    data: {
      media_id: 108275680, // 文件夹的id
      pn: 1,
      ps: 20,
      keyword: '',
      order: 'mtime',
      type: 0,
      tid: 0,
      platform: 'web'
    },
    method: 'GET'
  }

  const collection_folder_detail = {
    url: 'https://api.bilibili.com/x/space/fav/season/list',
    data: {
      season_id: 1632596, // 文件夹的id
      pn: 1,
      ps: 20
    }
  }

  async function getFavVideos() {
    const fav_videos = [];

    // 先获取文件夹列表
    const folder_res = await axios.get(fav_folders.url, {
      up_mid: 44120780,
    })

    /**
      folder_item
      {
        "id": 2571151980,
        "fid": 25711519,
        "mid": 44120780,
        "attr": 22,
        "title": "地质",
        "fav_state": 0,
        "media_count": 1
      }
     */
    const folders = folder_res.data?.data?.list; 

    // 遍历文件夹列表，取出视频信息
    // 需要注意一个文件夹中有多少个视频，每一页是20，可能需要多次请求不同页
    folders?.forEach((item) => {
      const video_res = axios.get(fac_folder_detail.url, {
        media_id: item.id,
        pn: 1,
        ps: 20,
        keyword: '',
        order: 'mtime',
        type: 0,
        tid: 0,
        platform: 'web'
      })

      video_res.data?.data?.medias?.forEach((item) => {
        fav_videos.push({
          id: item.id,
          title: item.title,
          cover: item.cover,
          link: item.link,
          bvid: item.bvid || item.bv_id,
        });
      })
    });
  }

  getFavVideos()
}

main();